﻿
using System.Collections.ObjectModel;
using System.Text;
using CommunityToolkit.Mvvm.Input;
using GalFrameWork.Models;
using GalFrameWork.Services;
using GalFrameWork.Utils;

namespace GalFrameWork.ViewModel;

public class MainPageViewModel {
    private IDialogStorage _dialogStorage;

    //【tips】alt+ins自动代码生成,Constructor
    public MainPageViewModel(IDialogStorage dialogStorage) {
        _dialogStorage = dialogStorage;
    }
    //读取数据
    public ObservableCollection<MyDialog> dialogs { get; } =
        new ObservableCollection<MyDialog>();

    private RelayCommand _initCommand;

    public RelayCommand InitCommand => _initCommand ??= new RelayCommand(async () =>
    {
        await _dialogStorage.Initialize();
    });

    private RelayCommand _addCommand;

    public RelayCommand AddCommand => _addCommand ??= new RelayCommand(async () =>
    {
        var dialog = new MyDialog
        {
            Id = 1,
            SceneId = 1,
            SpeakerName = "Gal",
            Sentence= "Hello GalWorld"
        };
        await _dialogStorage.AddDialog(dialog);
    });

    private RelayCommand _listCommand;
    
    public RelayCommand ListCommand => _listCommand ??= new RelayCommand(async () =>
        {
            var list = await  _dialogStorage.GetDialogs();
            foreach (var dialog in list)
            {
                dialogs.Add(dialog);
            }
           
        });

    //从Excel导入
    private RelayCommand _importCommand;

    public RelayCommand ImportCommand => _importCommand ??= new RelayCommand(async () => {
        OpenFileUtil openFileUtil = new OpenFileUtil();
        var result = await openFileUtil.GetExcelFile();
        if (result != null) {
            var file = result.FullPath;
            var list = ExcelUtil.ReadExcelToDialogs(file);
            await _dialogStorage.ImportDialogs(list);
            
        }
    });

    
}
